package com.lanxin.oa.fiancial.controller;

import com.lanxin.bean.*;
import com.lanxin.oa.fiancial.service.ApplyService;
import com.lanxin.util.PageBean;
import com.lanxin.util.TimeConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/apply")
public class ApplyController extends TimeConverter {

    @Autowired
    private ApplyService applyService;

    @RequestMapping("/selectByIdbxInfor")
    public ModelAndView selectByIdbxInfor(HttpSession session,Integer page, Integer pageSize,BxInformal bxInformal){
        PageBean<BxInformal> pageBean = applyService.getDeptPage(session,page, pageSize,bxInformal);
        ModelAndView mv = new ModelAndView();
        mv.addObject("pageBean", pageBean);
        mv.addObject("bxInformal", bxInformal);
        mv.setViewName("../view/apply/listApply.jsp");
        return mv;
    }

    @RequestMapping("/selectbxInfor")
    public ModelAndView selectbxInfor(Integer page, Integer pageSize,BxInformal bxInformal,String empName){
        PageBean<BxInformal> pageBean = applyService.getbxinFormailPage(page, pageSize,bxInformal,empName);
        ModelAndView mv = new ModelAndView();
        mv.addObject("pageBean", pageBean);
        mv.addObject("bxInformal",bxInformal);
        mv.addObject("empName",empName);
        mv.setViewName("../view/apply/allApply.jsp");
        return mv;
    }

    @RequestMapping("/selectregister")
    public ModelAndView selectregister(Integer page, Integer pageSize,BxInformal bxInformal,String empName){
        PageBean<BxInformal> pageBean = applyService.getregisterPage(page, pageSize,bxInformal,empName);
        ModelAndView mv = new ModelAndView();
        mv.addObject("pageBean", pageBean);
        mv.addObject("bxInformal",bxInformal);
        mv.addObject("empName",empName);
        mv.setViewName("../view/apply/register.jsp");
        return mv;
    }

    @RequestMapping("/toAddBxInformal")
    public ModelAndView  toAddBxInformal(){
        List<BxInformaltype> bxtype=applyService.selectBxType();
        List<LcFlow> lcFlows=applyService.selectLcFlow();
        ModelAndView mv = new ModelAndView();
        mv.addObject("bxtype", bxtype);
        mv.addObject("lcFlows", lcFlows);
        mv.setViewName("../view/apply/editapply.jsp");
        return mv;
    }

    @RequestMapping("/editBxInformal")
    public ModelAndView  editBxInformal(BxInformal bxInformal, HttpServletRequest request,@RequestParam(required = false) MultipartFile[] content,Integer[] typeid,double[] applyAmount,String[] description,Integer[] detailoid,Integer[] accessaryoid,String[] contentfile,String[] dname){
        applyService.insertBxInformal(bxInformal,request,content,typeid,applyAmount,description,detailoid,accessaryoid,contentfile,dname);
        ModelAndView mv = new ModelAndView();
        mv.setViewName("redirect:/apply/selectByIdbxInfor");
        return mv;
    }

    @RequestMapping("/BxInfo")
    public ModelAndView  BxInfo(Integer oid){
        BxInformal bx = applyService.BxInFormalById(oid);
        ModelAndView mv = new ModelAndView();
        mv.addObject("bx", bx);
        mv.setViewName("../view/apply/infoapply.jsp");
        return mv;
    }

    @RequestMapping("/updateEmpStatue")
    @ResponseBody
    public boolean updateEmpStatue(Integer  oid){
        return applyService.updateBxStatus(oid);
    }

    @RequestMapping("/toUpdateapply")
    public ModelAndView toUpdateapply(Integer oid){
        ModelAndView mv=new ModelAndView();
        BxInformal bx = applyService.BxInFormalById(oid);
        List<BxInformaltype> bxtype=applyService.selectBxType();
        List<LcFlow> lcFlows=applyService.selectLcFlow();
        mv.addObject("bxtype",bxtype);
        mv.addObject("lcFlows",lcFlows);
        mv.addObject("bx",bx);
        mv.setViewName("../view/apply/editapply.jsp");
        return mv;
    }


    @RequestMapping("/deleteBxInformal")
    @ResponseBody
    public boolean deleteBxInformal(Integer  oid){
        return applyService.deleteBxInformal(oid);
    }


    @RequestMapping("/register")
    public ModelAndView  register(Integer oid){
        BxInformal bx = applyService.BxInFormalById(oid);
        ModelAndView mv = new ModelAndView();
        mv.addObject("bx", bx);
        mv.setViewName("../view/apply/registerapply.jsp");
        return mv;
    }

    @RequestMapping("/payeeInformal")
    public ModelAndView  payeeInformal(BxInformal bxInformal,double[] amount,Integer[] doid){
         applyService.updatePayeeInformal(bxInformal,amount,doid);
        ModelAndView mv = new ModelAndView();
        mv.setViewName("../view/apply/register.jsp");
        return mv;
    }

    //调用下载
    @RequestMapping("/downloadBxFile")
    public void downloadBxFile(Integer oid, HttpServletResponse response, HttpServletRequest request){
            applyService.xiazai(oid,response,request);
    }
}
